<?php
/*
*用户模块
*/
class User_model extends CI_Model{
	public function __construct(){
		$this->load->database();
		
	}
	
//////////////单个用户////////////////////	

	//用户信息 , 角色信息 及其他
	public function userInfo($data){
		$this->db->select('id,openid,username,email,role_id,status');
		if($query = $this->db->get_where('user',$data)){
			return $query->row_array();
		}
		else{
			return false;
		}
	
	}
	
	//用户详细信息
	public function userProfile($id){
		$this->db->select('u.username,u.email,u.id,ut.*');
		$this->db->from('user u');
		$this->db->join('user_attr ut','u.id = ut.uid','left');
		$this->db->where(array('u.id'=>$id));
		$query = $this->db->get();
		return $query->row_array();
	}


//////////////////////用户读写操作

	
	//添加用户
	//$name,$password,$email,$role_id
	public function addUser($data){
		
		return ($this->db->insert('user',$data) && $this->db->insert('user_attr',array('uid'=>$data['id'])));
	}
	
	//编辑用户
	public function editUser($data,$where){
		$this->db->where($where);
		return $this->db->update('user',$data);
	}
	
	//编辑用户详细资料
	public function editProfile($data,$uid){
		$where = array('uid'=>$uid);
		$query = $this->db->get_where('user_attr',$where);
		if($query->row_array()){
			$this->db->where($where);
			return $this->db->update('user_attr',$data);
		}else{
			//添加uid
			$data['uid'] = $uid;
			return $this->db->insert('user_attr',$data);
		}
		
	}
	
	//更新密码
	public function editPassword($openid,$newpassword){
		$this->db->where(array('openid' => (int)$openid));
		return $this->db->update('user',array('password' => $newpassword));
	}
	
	//记录邮件发送历史
	public function addEmailLog($uid,$salt,$type='password'){
		
		$data = array(
			'uid' => $uid,
			'salt' => $salt,
			'type' => $type,
			'sendtime' => time(),
			'expiretime' => time()+45*60
		);
		return $this->db->insert('user_mail_list',$data);
	}
	
	//更新状态
	public function up_status($id,$status){
		
		$ids = explode(',',$id);
		$this->db->where_in('id',$ids);
		$this->db->update('user',$status);
		//Q: how to get the real affected_rows and the object
		//怎么获得受影响的行数以及具体行的信息
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	//删除用户
	public function deleteUser($id){
		
		//删除
		$ids = explode(',',$id);
		$this->db->where_in('id',$ids);
		$this->db->delete('user');
		$rows = $this->db->affected_rows();
		if($rows >= 1){
			return true;
		}else{
			return false;
		}

	}
	
	//用户权限
	public function userAction(){
	
	}
	
	//检测用户登录
	public function islogin(){
		$this->load->library('session');
		$userinfo = $this->session->userdata('user');
	
		if( ! $userinfo['username'] || (! $userinfo['id']  &&  ! $userinfo['openid'])){
			return false;
		}
		return true;
	}
	
	
	//登录
	public function login($data,$remember = false){
		$this->load->library('session');
	
		//获得用户基本信息
		if( $userinfo = $this->userInfo($data) ){
			
			$this->session->set_userdata('user',$userinfo);
			
			//记住登录信息
			if($remember){
				$this->session->set_userdata('remember_user',$userinfo);
			}
			return true;
		}else{
			return false;
		}
		
	} 
	
}